/*****************************************************************************
 * hal_efm32lg_dio.c
 *
 * Copyright (C) 2020 Jeasonvor 1101627719@qq.com
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 *
 ****************************************************************************/

/*****************************************************************************
 * Included Files
 ****************************************************************************/
#include "plum_api.h"
#include "hal/source/hal_efm32lg/hal_efm32lg.h"
/*****************************************************************************
 * Trace Definitions
 ****************************************************************************/
#define LOG_RUN_LEVEL LOG_LEVEL_NONE
#define LOG_MODULE    "hal.dio"
#include "thirdparty/log/log.h"
/*****************************************************************************
 * Pre-processor Definitions
 ****************************************************************************/

/*****************************************************************************
 * Private Types
 ****************************************************************************/

typedef struct {
    plum_u32         idx;
    dio_io_t         io;
    struct list_head list;
} hal_efm32_dio_t;
/*****************************************************************************
 * Private Function Prototypes
 ****************************************************************************/

/*****************************************************************************
 * Private Data
 ****************************************************************************/

PLUM_PRIVATE hal_efm32_dio_t dio_head = {0};
/*****************************************************************************
 * Public Data
 ****************************************************************************/

/*****************************************************************************
 * Private Functions
 ****************************************************************************/

PLUM_PRIVATE
hal_efm32_dio_t *hal_efm32lg_dio_node_apply(plum_u32 id, dio_io_t *io)
{
    hal_efm32_dio_t *node = plum_null;

    do {
        if (!dio_head.list.next) {
            INIT_LIST_HEAD(&dio_head.list);
        }

        node = (hal_efm32_dio_t *)EFM32LG_MALLOC(sizeof(hal_efm32_dio_t));
        if (!node) {
            LOG_E("dio malloc err");
            break;
        }

        node->idx       = PLUM_HAL_DIO_IDX(id);
        node->io.di_id  = io.di_id;
        node->io.do_id  = io.do_id;
        node->io.clk_id = io.clk_id;
    } while (0);

    return (node);
}

PLUM_PRIVATE
hal_efm32_dio_t *hal_efm32lg_dio_node_find(plum_u32 id)
{
    hal_efm32_dio_t *node = NULL;

    struct list_head *pos;
    list_for_each(pos, &dio_head.list)
    {
        node = list_entry(pos, hal_efm32_dio_t, list);
        if (node->hdl.dio_idx == PLUM_HAL_DIO_IDX(id)) {
            break;
        }
    }

    return (node);
}

PLUM_PRIVATE
plum_s32 hal_efm32lg_dio_node_insert(hal_efm32_dio_t *node)
{
    plum_s32 rc = PLUM_ECODE_EPARA;

    if (node) {
        list_add(&node->list, &dio_head.list);
        rc = PLUM_ECODE_OK;
    }

    return (rc);
}

PLUM_PRIVATE
plum_s32 hal_efm32lg_dio_node_del(hal_efm32_dio_t *node)
{
    plum_s32 rc = PLUM_ECODE_OK;

    do {
        if (!node) {
            rc = PLUM_ECODE_EPARA;
            break;
        }

        list_del(&node->list);
        plum_hal_malloc(node);
    } while (0);

    return (rc);
}

PLUM_PRIVATE
plum_s32 hal_efm32lg_dio_write_byte(hal_efm32_dio_t *node, plum_u8 value)
{
    plum_s32 rc = PLUM_ECODE_OK;

    plum_u8 loop;
    for (loop = 0; loop < 8; loop++) {
        plum_hal_gpio_write(node->io.clk_id, PLUM_HAL_GPIO_LOW);
        if (value & (1 << loop)) {
            plum_hal_gpio_write(node->io.do_id, PLUM_HAL_GPIO_HIGH);
        }
        else {
            plum_hal_gpio_write(node->io.do_id, PLUM_HAL_GPIO_LOW);
        }
        plum_hal_gpio_write(node->io.clk_id, PLUM_HAL_GPIO_HIGH);
    }

    return (rc);
}

PLUM_PRIVATE
plum_s32 hal_efm32lg_dio_read_byte(hal_efm32_dio_t *node)
{
    plum_s32 rc = 0;

    plum_u8 loop;
    for (loop = 0; loop < 8; loop++) {
        plum_hal_gpio_write(node->io.clk_id, PLUM_HAL_GPIO_LOW);
        rc |= plum_hal_gpio_read(node->io.di_id) << loop;
        plum_hal_gpio_write(node->io.clk_id, PLUM_HAL_GPIO_HIGH);
    }

    return (rc);
}

/*****************************************************************************
 * Public Functions
 ****************************************************************************/

PLUM_PUBLIC
plum_s32 plum_hal_dio_init(plum_u32 id, plum_hal_dio_cof_t *cof)
{
    plum_s32 rc = PLUM_ECODE_OK;

    do {
        if (!PLUM_HAL_ID_IS_DIO(id)) {
            rc = PLUM_ECODE_EPARA;
            break;
        }

        if (!cof) {
            rc = PLUM_ECODE_INVALID;
            break;
        }

        plum_hal_gpio_cof_t cof = {
            .mode       = PLUM_HAL_GPIO_MODE_COMMON,
            .config.com = PLUM_HAL_GPIO_MODE_OUT_PP,
        };
        rc = plum_hal_gpio_init(cof->io.clk_id, &cof);
        if (rc) {
            LOG_E("init gpio[clk] err,rc: %d", rc);
            break;
        }

        rc = plum_hal_gpio_init(cof->io.do_id, &cof);
        if (rc) {
            LOG_E("init gpio[do] err,rc: %d", rc);
        }

        cof.config.com = PLUM_HAL_GPIO_MODE_INPUT;
        rc             = plum_hal_gpio_init(cof->io.di_id, &cof);
        if (rc) {
            LOG_E("init gpio[di] err,rc: %d", rc);
        }

        hal_efm32_dio_t *node = hal_efm32lg_dio_node_apply(id, &cof->io);
        if (!node) {
            LOG_E("hal_efm32lg_dio_node_apply err");
            rc = PLUM_ECODE_EMEM;
            break;
        }

        rc = hal_efm32lg_dio_node_insert(node);
        if (rc) {
            LOG_E("hal_efm32lg_dio_node_insert err,rc: %d", rc);
            break;
        }
    } while (0);

    return (rc);
}

PLUM_PUBLIC
plum_s32 plum_hal_dio_read_byte(plum_u32 id)
{
    plum_s32 rc = PLUM_ECODE_OK;

    do {
        if (!PLUM_HAL_ID_IS_DIO(id)) {
            rc = PLUM_ECODE_EPARA;
            break;
        }

        hal_efm32_dio_t *node = hal_efm32lg_dio_node_find(id);
        if (!node) {
            rc = PLUM_ECODE_INVALID;
            LOG_E("the dio interface not inited,rc: %d", rc);
            break;
        }

        rc = hal_efm32lg_dio_read_byte(node);
        if (rc < 0) {
            LOG_E("hal_efm32lg_dio_read_byte err,rc: %d", rc);
            break;
        }
        LOG_D("hal_efm32lg_dio_read_byte : 0x02X", rc);
    }

    return (rc);
}

PLUM_PUBLIC
plum_s32 plum_hal_dio_write_byte(plum_u32 id, plum_u8 value)
{
    plum_s32 rc = PLUM_ECODE_OK;

    do {
        if (!PLUM_HAL_ID_IS_DIO(id)) {
            rc = PLUM_ECODE_EPARA;
            break;
        }

        hal_efm32_dio_t *node = hal_efm32lg_dio_node_find(id);
        if (!node) {
            rc = PLUM_ECODE_INVALID;
            LOG_E("the dio interface not inited,rc: %d", rc);
            break;
        }

        rc = hal_efm32lg_dio_write_byte(node, value);
        if (rc) {
            LOG_E("hal_efm32lg_dio_write_byte err,rc:%d", rc);
            break;
        }
        LOG_D("hal_efm32lg_dio_write_byte : 0x%02X", value);
    }

    return (rc);
}

PLUM_PUBLIC
plum_s32 plum_hal_dio_read(plum_u32 id, plum_u32 addr, plum_u8 *rData,
                           plum_u32 len)
{
    plum_s32 rc = PLUM_ECODE_OK;

    do {
        if (!PLUM_HAL_ID_IS_DIO(id)) {
            rc = PLUM_ECODE_EPARA;
            break;
        }

        hal_efm32_dio_t *node = hal_efm32lg_dio_node_find(id);
        if (!node) {
            rc = PLUM_ECODE_INVALID;
            LOG_E("the dio interface not inited,rc: %d", rc);
            break;
        }
    }

    return (rc);
}

PLUM_PUBLIC
plum_s32 plum_hal_dio_write(plum_u32 id, plum_u32 addr, plum_u8 *wData,
                            plum_u32 len)
{
    plum_s32 rc = PLUM_ECODE_OK;

    do {
        if (!PLUM_HAL_ID_IS_DIO(id)) {
            rc = PLUM_ECODE_EPARA;
            break;
        }

        hal_efm32_dio_t *node = hal_efm32lg_dio_node_find(id);
        if (!node) {
            rc = PLUM_ECODE_INVALID;
            LOG_E("the dio interface not inited,rc: %d", rc);
            break;
        }
    }

    return (rc);
}

/****************************************************************************/
/*                                                                          */
/*  End of file.                                                            */
/*                                                                          */
/****************************************************************************/
